package com.ruoyi.fantasy.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huanzhidadi.fantasy.bean.domain.FantasyCity;
import com.huanzhidadi.fantasy.common.constant.RedisConstants;
import com.ruoyi.fantasy.common.WorldInit;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.fantasy.domain.FantasyItemTypeBO;
import com.ruoyi.fantasy.utils.RedisUtil;
import com.ruoyi.system.domain.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fantasy.mapper.FantasyItemtypeMapper;
import com.ruoyi.fantasy.service.IFantasyItemtypeService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 道具类型 服务层实现
 * 
 * @author ruoyi
 * @date 2018-12-25
 */
@Service
public class FantasyItemtypeServiceImpl implements IFantasyItemtypeService 
{
	@Resource
	private FantasyItemtypeMapper fantasyItemtypeMapper;

	@Autowired
	private RedisUtil redisUtil;

	/**
     * 查询道具类型信息
     * 
     * @param id 道具类型ID
     * @return 道具类型信息
     */
    @Override
	public FantasyItemType selectFantasyItemtypeById(String id)
	{
	    return fantasyItemtypeMapper.selectFantasyItemtypeById(id);
	}
	
	/**
     * 查询道具类型列表
     * 
     * @param fantasyItemtype 道具类型信息
     * @return 道具类型集合
     */
	@Override
	public List<FantasyItemType> selectFantasyItemtypeList(FantasyItemType fantasyItemtype)
	{
		Map<String, String> itemTypes = redisUtil.hmGet(RedisConstants.FANTASY_ITEM_TYPE);
		FantasyItemTypeBO fantasyItemTypeBO = JSONObject.parseObject(itemTypes.get("8"), FantasyItemTypeBO.class);
	    return fantasyItemtypeMapper.selectFantasyItemtypeList(fantasyItemtype);
	}
	
    /**
     * 新增道具类型
     * 
     * @param fantasyItemtype 道具类型信息
     * @return 结果
     */
	@Override
	public int insertFantasyItemtype(FantasyItemType fantasyItemtype)
	{
		Integer result = fantasyItemtypeMapper.insertFantasyItemtype(fantasyItemtype);
		if (result > 0) {
			if (fantasyItemtype.getGround() == 1) {
				FantasyItemTypeBO fantasyItemTypeBO = new FantasyItemTypeBO();
				BeanUtils.copyProperties(fantasyItemtype, fantasyItemTypeBO);
				redisUtil.hPut(RedisConstants.FANTASY_ITEM_TYPE, fantasyItemTypeBO.getId(), JSON.toJSONString(fantasyItemTypeBO));
			}
		}
	    return result;
	}
	
	/**
     * 修改道具类型
     * 
     * @param fantasyItemtype 道具类型信息
     * @return 结果
     */
	@Override
	public int updateFantasyItemtype(FantasyItemType fantasyItemtype)
	{
		Integer result = fantasyItemtypeMapper.updateFantasyItemtype(fantasyItemtype);
		if (result > 0) {
			if (fantasyItemtype.getGround() == 1) {
				fantasyItemtype = fantasyItemtypeMapper.selectFantasyItemtypeById(fantasyItemtype.getId());
				FantasyItemTypeBO fantasyItemTypeBO = new FantasyItemTypeBO();
				BeanUtils.copyProperties(fantasyItemtype, fantasyItemTypeBO);
				redisUtil.hPut(RedisConstants.FANTASY_ITEM_TYPE, fantasyItemTypeBO.getId(), JSON.toJSONString(fantasyItemTypeBO));
			}
		}
	    return result;
	}

	/**
     * 删除道具类型对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteFantasyItemtypeByIds(String ids)
	{
		Integer result = fantasyItemtypeMapper.deleteFantasyItemtypeByIds(Convert.toStrArray(ids));
		if (result > 0) {
			redisUtil.hDelete(RedisConstants.FANTASY_ITEM_TYPE, ids.split(","));
		}
		return result;
	}

	/**
	 * 转换列表中的值为对应的内容
	 * @param list
	 */
	@Override
	public void transforData(List<FantasyItemType> list) {
		Map<String, FantasyCity> citiesMap = WorldInit.citiesMap;
		Map<String, SysUser> sysUserMap = WorldInit.sysUserMap;
		for (FantasyItemType fantasyItemtype : list) {
			if (fantasyItemtype.getBelongsCity() != null) {
				if (citiesMap.get(fantasyItemtype.getBelongsCity()) != null) {
					//TODO 需转BO
					fantasyItemtype.setCityname(citiesMap.get(fantasyItemtype.getBelongsCity()).getCityname());
				} else {
					//fantasyItemtype.setCityName(fantasyItemtype.getBelongsCity().toString());
					fantasyItemtype.setCityname("全部");
				}
			}
			if (fantasyItemtype.getCreateBy() != null) {
				if (sysUserMap.get(fantasyItemtype.getCreateBy()) != null) {
					fantasyItemtype.setCreateBy(sysUserMap.get(fantasyItemtype.getCreateBy()).getUserName());
				} else {
					fantasyItemtype.setCreateBy("系统管理员");
				}
			}
			if (fantasyItemtype.getUpdateBy() != null) {
				if (sysUserMap.get(fantasyItemtype.getUpdateBy()) != null) {
					fantasyItemtype.setUpdateBy((sysUserMap.get(fantasyItemtype.getUpdateBy()).getUserName()));
				} else {
					fantasyItemtype.setUpdateBy("系统管理员");
				}
			}
			//禁用标记
			if (fantasyItemtype.getDelFlag().equals("0")) {
				fantasyItemtype.setDelFlag("正常");
			} else if (fantasyItemtype.getDelFlag().equals("1")) {
				fantasyItemtype.setDelFlag("禁用");
			}
			//稀有
			//TODO 需转BO
			if (fantasyItemtype.getRarely().equals(0)) {
				fantasyItemtype.setRarelyStr("否");
			} else if (fantasyItemtype.getRarely().equals(1)) {
				fantasyItemtype.setRarelyStr("是");
			}
			//装备
			if (fantasyItemtype.getGround().equals(0)) {
				fantasyItemtype.setGroundStr("否");
			} else if (fantasyItemtype.getGround().equals(1)) {
				fantasyItemtype.setGroundStr("是");
			}
			//装备
			if (fantasyItemtype.getEquip().equals(0)) {
				fantasyItemtype.setEquipStr("否");
			} else if (fantasyItemtype.getEquip().equals(1)) {
				fantasyItemtype.setEquipStr("是");
			}
			//使用
			if (fantasyItemtype.getUsable().equals(0)) {
				fantasyItemtype.setUsableStr("否");
			} else if (fantasyItemtype.getUsable().equals(1)) {
				fantasyItemtype.setUsableStr("是");
			}
			//安装
			if (fantasyItemtype.getPlacement().equals(0)) {
				fantasyItemtype.setPlacementStr("否");
			} else if (fantasyItemtype.getPlacement().equals(1)) {
				fantasyItemtype.setPlacementStr("是");
			}
			//任务
			if (fantasyItemtype.getTask().equals(0)) {
				fantasyItemtype.setTaskStr("否");
			} else if (fantasyItemtype.getTask().equals(1)) {
				fantasyItemtype.setTaskStr("是");
			}
		}

	}

	@Override
	public int disableFantasyItemtypeByIds(String ids) {
		Integer result = fantasyItemtypeMapper.disableFantasyItemtypeByIds(Convert.toStrArray(ids));
		if (result > 0) {
			redisUtil.hDelete(RedisConstants.FANTASY_ITEM_TYPE, ids.split(","));
		}
		return result;
	}

	@Override
	public int restoreFantasyItemtypeByIds(String ids) {
		Integer result = fantasyItemtypeMapper.restoreFantasyItemtypeByIds(Convert.toStrArray(ids));
		if (result > 0) {
			//redisUtil.hDelete(RedisConstants.FANTASY_ITEM_TYPE, ids.split(","));
			String[] idArray = ids.split(",");
			for (int i=0; i<idArray.length; i++) {
				FantasyItemType fantasyItemtype = fantasyItemtypeMapper.selectFantasyItemtypeById(idArray[i]);
				if (fantasyItemtype.getGround() == 1) {
					FantasyItemTypeBO fantasyItemTypeBO = new FantasyItemTypeBO();
					BeanUtils.copyProperties(fantasyItemtype, fantasyItemTypeBO);
					redisUtil.hPut(RedisConstants.FANTASY_ITEM_TYPE, fantasyItemTypeBO.getId(), JSON.toJSONString(fantasyItemTypeBO));
				}
			}
		}
		return result;
	}

	@Override
	public List<FantasyItemType>  selectFantasyItemtypeListRemoveAllocated(FantasyItemType fantasyItemtype) {
		return fantasyItemtypeMapper.selectFantasyItemtypeListRemoveAllocated(fantasyItemtype);
	}

	@Override
	public void cacheFantasyItemtypeList()
	{
		FantasyItemType queryItemType = new FantasyItemType();
		queryItemType.setGround(1);
		List<FantasyItemType> fantasyItemTypeList = fantasyItemtypeMapper.selectFantasyItemtypeList(queryItemType);
		for (FantasyItemType fantasyItemType : fantasyItemTypeList) {
			FantasyItemTypeBO fantasyItemTypeBO = new FantasyItemTypeBO();
			BeanUtils.copyProperties(fantasyItemType, fantasyItemTypeBO);
			redisUtil.hPut(RedisConstants.FANTASY_ITEM_TYPE, fantasyItemTypeBO.getId(), JSON.toJSONString(fantasyItemTypeBO));
		}
	}
}
